<?php

namespace App\Http\Controllers\Api\NewWebsite;

use  App\Http\Resources\NewWebSite\WorkshopCollection;
use App\Http\Resources\NewWebSite\WorkShopResource;
use App\Interfaces\WorkshopInterFace;
use App\model\Workshop;
use App\Repository\WorkShopRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Carbon\Carbon;
use App\BrowseRecords;
use App\Http\Requests\NewWebsiteCounselRequest;
use App\Http\Requests\NewWebsiteYuyueRequest;
use App\Http\Resources\Agent\CityCollection;
use App\Http\Resources\AreaCollection;
use App\Http\Resources\CommentCollection;

use App\Http\Resources\NewWebSite\CityCollection as NewWebSiteCityCollection;
use App\Mail\ZiXunMail;
use App\model\agent\AgentCounse;
use App\model\Area;
use App\model\City;
use App\UserSearch;
use App\model\ImSet;
use App\model\panyuan\WorkshopYuyue;
use App\model\WorkshopHangye;
use App\model\WorkshopIndustry;
use App\model\WorkshopPurpose;
use App\model\WorkshopTag;
use Doctrine\DBAL\Types\Types;

class WorkshopController extends Controller
{
    public function index(Request $request,WorkshopInterFace $workShopRepository){


     //   $user_id = auth('api')->id();
      /*  if(isset($request['search_key'])&&$request['search_key']!=''&&isset($user_id)){
            $now = Carbon::now();
            UserSearch::updateOrCreate(['user_id'=>$user_id,'search_key'=>$request['search_key']],['updated_at'=>$now]);
        }*/
        $workShopList=$workShopRepository->DiskSourceList($request);
        return new WorkShopCollection($workShopList);
    }
    public function workshopRecommend(WorkshopInterFace $workshopInterFace){
        $workshop= $workshopInterFace->workshopRecommd(10);
        return new WorkshopCollection($workshop);
    }

        /**
     * 提交咨询
     * @param Request $request
     */
    public function counsel(NewWebsiteCounselRequest $request){

        $agent= new AgentCounse();
        $agent->name=$request->name;
        $agent->mobile=$request->mobile;
        $agent->sex=$request->sex;
        $agent->quyu_id=$request->city_id;
        $agent->area_id=$request->area_id;
        $agent->save();
       \Mail::to('1598095991@qq.com')->queue(new ZiXunMail($agent));
        return response()->json(
            [
                'code'=>0,
                'message'=>'提交成功'
            ]
        );
     }
     public function city(){
        $city=  City::whereIn('parent_id',[101,102])->orderBy('order','desc')->get()->map(function($item){


              return  [
                  'city_id'=>$item->id,
                  'name'=>$item->title
              ];

        });

      return response()->json(
        [
            'code'=>0,
            'message'=>'成功',
            'data'=>$city
        ]
        );
     }
     public function area(){
         $area=Area::all();
         return new AreaCollection($area);
     }
    public function detail(Request $request,WorkShopRepository $workShopRepository){
        $workshop=$workShopRepository->detail($request->workshop_id);

        return new  WorkShopResource($workshop);
    }
    //厂房筛选区镇信息
    public function workshopCity(Request $request){
        $city=  City::whereIn('parent_id',[101,102])->with('children')->orderBy('order','desc')->get();
        return new NewWebSiteCityCollection($city);
    }
    //盘源list
    public function list(Request $request){
        $types=$request->get('types',0);
        if($types==0){
            $types=[1];
        }else{
            $types=[5];
        }
        $is_sale=$request->get('is_sale',0);
        $sort_key = $request->get('sort_key', 'id');
        $sort=$request->get('sort','desc');
        $province_id = $request->get('province_id', '');
        $city_id = $request->get('city_id', '');
        $qu_id = $request->get('second_city_id', '');
        $tags = $request->get('tag_ids', '');
        $hangye = $request->get('industry_ids', '');
        $industry = $request->get('type_ids', '');
        $purpose = $request->get('purpose_ids', '');
        $area=$request->get('area_id','');
        $status=$request->get('status');
        $floor=$request->get('floor');
        $workshop = Workshop::with('hangye', 'qu', 'industry', 'tags','park');
        $workshop=$workshop->orderBy('is_zhiding','desc');
        $workshop = $workshop->whereIn('type',[2,$is_sale]);
        if ($sort_key) {
            if($sort_key=='price'&&$is_sale===0){
                $sort_key='rent';
            }
            $workshop = $workshop->orderBy($sort_key,$sort);
        };

        if ($province_id) {
            $workshop = $workshop->where('p_province_id', $province_id);
        }
        if ($city_id) {
            $workshop = $workshop->where('p_city_id', $city_id);
        }
        if ($qu_id) {
            $workshop = $workshop->where('p_district_id', $qu_id);
        }
        if ($floor){
            $workshop=$workshop->where('floor', $floor);
        }
        //标签
        if ($tags) {
            $workshop = $workshop->whereHas('tags', function ($builder) use ($tags) {
                $tags = explode(',', $tags);
                $builder->whereIn('workshop_tag.id', $tags);
            });
        }
        //行业
        if ($hangye) {
            $workshop = $workshop->whereHas('hangye', function ($builder) use ($hangye) {
                $hangye = explode(',', $hangye);
                $builder->whereIn('workshop_hangye.id', $hangye);
            });
        }
        //类型
        if ($industry) {
            $workshop = $workshop->whereHas('industry', function ($builder) use ($industry) {
                $industry = explode(',', $industry);
                $builder->whereIn('workshop_industry.id', $industry);
            });
        }
        //用途
        if ($purpose) {

                $workshop = $workshop->whereHas('purpose', function ($builder) use ($purpose,$types) {
                    $purpose = explode(',', $purpose);
                    $purpose =array_merge($purpose,$types);
                    $builder->whereIn('workshop_purpose.id', $purpose);
                });
        }else{
            $workshop = $workshop->whereHas('purpose', function ($builder) use ($types) {
                $purpose =$types;
                $builder->whereIn('workshop_purpose.id', $purpose);
            });
        }
        //面积
        if ($area){

            switch ($area) {
                case 1:
                    $workshop = $workshop->where('area', '<', 1000);
                    break;
                case 2:
                    $workshop = $workshop->where('area', '>=', 1000)
                        ->where('area', '<=', 3000);
                    break;
                case  3:
                    $workshop = $workshop->where('area', '>=', 3000)
                        ->where('area', '<=', 6000);
                    break;
                case  4:
                    $workshop = $workshop->where('area', '>=', 6000)
                        ->where('area', '<=', 10000);
                    break;
                case  5:
                    $workshop = $workshop->where('area', '>=', 10000);
                    break;
            }
        }
        //状态
        if ($status!==""&&$status!==null){
            $workshop=$workshop->where('status',$status);
        }
       $workshop= $workshop->paginate($request->get('page_size',10));
       return new WorkshopCollection($workshop);
    }
    /**
     * 行业
     */
    public function hangye(){
        $workshop= WorkshopHangye::all();
        return [
            'code'=>0,
            'data'=> $workshop->map(function($item){

                return [
                     'industry_id'=>$item->id,
                     'name'=>$item->name
                ];

            })
        ];
    }
    /**
     * tags
     */
    public function tag(){
        $workshop= WorkshopTag::all();
        return [
            'code'=>0,
            'data'=> $workshop->map(function($item){

                return [
                     'tag_id'=>$item->id,
                     'name'=>$item->name
                ];

            })
        ];

    }
        /**
     * 用途
     */
    public function industry(){
        $workshop=  WorkshopIndustry::all();
        return [
            'code'=>0,
            'data'=> $workshop->map(function($item){

                return [
                     'type_id'=>$item->id,
                     'name'=>$item->name
                ];

            })
        ];
      }
      //
      public function purpose(){
          $workshop=  WorkshopPurpose::all();
          return [
            'code'=>0,
            'data'=> $workshop->map(function($item){

                return [
                     'purpose_id'=>$item->id,
                     'name'=>$item->name
                ];

            })
        ];
      }
      public function yuyue(NewWebsiteYuyueRequest $request){
           $workshop= WorkshopYuyue::whereMobile($request->mobile)->first();
           if($workshop){
               return response()->json(
                [
                    'code'=>'error',
                    'message'=>'您已经提交过',

                ]);
           }else{
               $workshop= new WorkshopYuyue();
               $workshop->name=$request->name;
               $workshop->mobile=$request->mobile;
               $workshop->start_time=$request->start_time;
               $workshop->end_time=$request->end_time;
               $workshop->workshop_id=$request->workshop_id;
               $workshop->save();
               return response()->json(
                [
                    'code'=>0,
                    'message'=>'提交成功',

                ]);
           }
      }

}
